package com.file.transfers.cmd;

import java.io.File;
import java.util.Scanner;

import com.file.transfers.Main;
import com.file.transfers.util.FileUtil;

public class Cmd {
	public static String IP;
	public static int Port;
	public static int FilePath;
	public static Scanner scanner;
	public static void main(String[] args) {
		initData();
		System.out.println("欢迎使用java版文件传输！");
		showList();
		//selectJson();
		//start();
		
	}
	public static void showList(){
		System.out.println("0：默认启动文件服务(包含接受和发送)");
		System.out.println("1：启动接受文件服务");
		System.out.println("2：启动发送文件服务");
		System.out.println("3：退出");
		int count = 0;
		try {
			count = scanner.nextInt();
		} catch (Exception e) {
			System.out.println("无效输入");
			showList();
			//e.printStackTrace();
		}
		try {
			switch (count) {
			case 0:
				defaultStart();
				break;
			case 1:
				startServer();
				break;
			case 2:
				showClientList();
				break;
			case 3:
				exit();
				break;
			default:
				System.out.println("无效输入");
				showList();
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			showList();
		}
	}
	public static void defaultStart(){
		Main.startServer(0,"");
		showClientList();
	}
	public static void startServer(){
		Main.startServer(0,"");
	}
	public static void showClientList(){
		readClientRemoteIPAndPort();
		String readClientFilePath = readClientFilePath();
		readClientSendFile(readClientFilePath);
	}
	public static void sendFile2(){
		String readClientFilePath = readClientFilePath();
		readClientSendFile(readClientFilePath);
	}
	public static void initData(){
		scanner=new Scanner(System.in);
	}
	public static void exit(){
		System.out.println("欢迎下次使用！");
		System.exit(1);
	}
	public static void readClientRemoteIPAndPort(){
		System.out.println("请输入对方IP地址及端口(IP:Port)：");
		String ipport = scanner.next();
		String[] split = ipport.split(":");
		IP=split[0];
		Port=Integer.parseInt(split[1]);
		Main.startClient(IP, Port);
	}
	public static String readClientFilePath(){
		System.out.println("请输入要发送文件或文件夹得路径：");
		String filePath = scanner.next();
		boolean exists = FileUtil.isExists(filePath);
		if(!exists){
			System.out.println("文件或文件夹不存在，请重新输入");
			filePath=readClientFilePath();
		}
		return filePath;
		
	}
	public static void readClientSendFile(String filePath){
		int preSendFiles = Main.fileTransfersClient.preSendFiles(filePath);
		switch (preSendFiles) {
		case 0:
			System.out.println("发送完成");
			clientSendOver();
			break;
		case 1:
			System.out.println("文件或文件夹不存在");
			break;
		default:
			break;
		}
	}
	public static void clientSendOver(){
		System.out.println("发送文件完成。");
		System.out.println("1：继续发送");
		System.out.println("2：返回上一级");
		System.out.println("3：退出");
		int tmp1 = scanner.nextInt();
		switch (tmp1) {
		case 1:
			sendFile2();
			break;
		case 2:
			showList();
			break;
		case 3:
			exit();
			break;
		default:
			System.out.println("未知输入");
			break;
		}	
	}
	public static void readFilePath(){
		System.out.println("请输入文件路径：");
		String filePath = scanner.next();
		boolean fileExist = FileUtil.isExists(filePath);
		if(!fileExist){
			System.out.println("输入的文件路径不存在,是否自动创建：");
			System.out.println("1：是");
			System.out.println("2：否(重新输入)");
			System.out.println("3：返回上一级");
			int tmp1 = scanner.nextInt();
			switch (tmp1) {
			case 1:
					File file = new File(filePath);
					boolean exists = file.exists();
					if(!exists){
						file.mkdirs();
					}
				break;
			case 2:
				readFilePath();
				break;
			case 3:
				readFilePath();
				break;
			default:
				System.out.println("未知输入");
				break;
			}
		}
	}
}
